<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>自动生成li基础版</title>
    <style>
        html,
        body,
        ul,
        li {
            margin: 0;
            padding: 0;
        }
        
        div {
            padding: 20px;
        }
        
        ul {
            margin-top: 40px;
        }
        
        li {
            list-style-type: none;
        }
        
        .clearfix::after {
            display: block;
            height: 0;
            content: "";
            clear: both;
            visibility: hidden;
        }
        
        .square {
            float: left;
            margin-left: 20px;
            width: 50px;
            height: 50px;
            font-size: 30px;
            line-height: 50px;
            color: #000;
            text-align: center;
        }
        
        .square:first-child {
            margin-left: 0;
        }
        
        .red {
            background-color: red;
        }
        
        .yellow {
            background-color: yellow;
        }
        
        .blue {
            background-color: blue;
        }
        
        .green {
            background-color: green;
        }
    </style>
</head>

<body>
    <div>
        <button id="btn">自动生成10个li</button>
        <ul id="liList" class="clearfix">
        </ul>
    </div>
    <script>
        var liList = document.getElementById('liList');
        var btn = document.getElementById('btn');
        btn.onclick = function() {
            for (var i = 0; i < 10; i++) {
                if (i == 0 && liList.innerHTML !== '') {
                    liList.innerHTML = '';
                }
                switch (i % 4) {
                    case 0:
                        liList.innerHTML += '<li class="square red">' + i + '</li>';
                        break;
                    case 1:
                        liList.innerHTML += '<li class="square yellow">' + i + '</li>';
                        break;
                    case 2:
                        liList.innerHTML += '<li class="square blue">' + i + '</li>';
                        break;
                    case 3:
                        liList.innerHTML += '<li class="square green">' + i + '</li>';
                        break;
                    default:
                        break;
                }
            }
        }
    </script>
</body>

</html>